<?php

namespace App\Jobs\Middleware;

use Closure;
use Illuminate\Support\Facades\Redis;

class RateLimited
{
    /**
     * 处理队列任务
     *
     * @param \Closure(object): void $next
     */
    public function handle(object $job, Closure $next): void
    {
        Redis::throttle('rate_limit_key')
            ->block(0)->allow(5)->every(1)
            ->then(function () use ($job, $next) {
                // 已获得锁...
                $next($job);
            }, function () use ($job) {
                // 没有获取到锁...
                $job->release(3);
            });
    }
}
